function addtolist(deal_id){
	callBlockUI();
	$.post(URL_DEFAULT + 'shoppingcart/add/', {'deal_id': deal_id}, function(data){
			$.unblockUI();	
			window.location = "/shoppingcart";
		});	
}

function doCheckout(){
	$.blockUI();
	$('[name=checkoutForm]').submit();
}

function doCheckoutResponse(responseText, statusText, xhr, $form){
	$.unblockUI();
	var data = eval('(' + responseText + ')');
	$('#checkoutFormDIV').html(data.content);
	if(data.success){
        if (typeof data.url_paypal != "undefined")
        {
            window.location = data.url_paypal;
            
        }
        else
        {
            window.location = "shoppingcart/receipt/" + data.order_data.id + "/" + 
		                  data.order_data.access_code;
                          
    		//if(data.message != null){
    		//	$('#resultDiv').html(data.message);
    		//}    
        }
		
    }else{
    	if(data.commonResponse.errorDescription != null){
    		$('#errorDiv').html(data.commonResponse.errorDescription);
    	}
    }
}

function clearcart(){
    if(confirm("Do you want to delete all item?")){
    	callBlockUI();
    	$.post(URL_DEFAULT + 'shoppingcart/clear/', {}, function(data){
    		$.unblockUI();	
    		window.location = "/shoppingcart";
    	});
    }	
}

function checkIsBuyMorePoint(){
	clearInterval(isBuyMorePoint);
	var isBuyMorePoint = setInterval(function() {
		$.post(URL_DEFAULT + 'game/getCurrentBalance', {'ajax_request' : true}, function(data){
			var total_order = $('#totalContainer').children().html().replace(/,/g, "");
			if(total_order > 0 && parseInt(total_order) <= parseInt(data)){
				$('#submitorder_div').show();
				$('#buymorepoint_div').hide();
				
				$('#balance_after_order_tr').show();
				$('#current_balance_span').html(data);
				$('#balance_after_order_span').html(data - total_order);
				
				clearInterval(isBuyMorePoint);
			}
		});
	}, 7000);	
}

function deleteCartItem(rowid){
	if(confirm("Do you want to delete this item?")){
		$.post(URL_DEFAULT + 'shoppingcart/deleteCartItem', {'rowid' : rowid}, function(data){
			$('#Content').html(data);
		});
	}
}

function changeCartItemQty(rowid, qty){
	$.post(URL_DEFAULT + 'shoppingcart/changeCartItemQty', {'rowid' : rowid, 'qty': qty}, function(data){
		$('#Content').html(data);
		//window.location = URL_DEFAULT + 'shoppingcart';
	});
}

/************ PAYMENT ********************/
// Shorthand for $( document ).ready()
$(function() {
    /*http://jqueryvalidation.org/*/
    $("#paypal_form").validate({
		rules: {
            txtEmailR: {
				required: true,
				email: true
			},
        },
		messages: {
			txtEmailR: "Please enter a valid email address",
        }
    });
    $("#cc_form").validate({
		rules: {
            cc_card_name: { required: true, },
            cc_card_number: { required: true, },
            cc_expiration_month: { required: true, },
            cc_expiration_year: { required: true, },
            cc_cvc: {
                minlength: 3,
                required: true, 
            },
            cc_email_recevied: {
				required: true,
				email: true
			},
        },
		messages: {
		    cc_card_name: "Please provide card name",
            cc_card_number: "Please provide card number",
            cc_expiration_month: "Please choose expiration month",
            cc_expiration_year: "Please choose expiration year",
            cc_cvc: {
                minlength: "CVC must be at least 3 characters long",
                required: "Please provide a password", 
            },
			cc_email_recevied: "Please enter a valid email address",
        }
    });
    $("#cc_card_number").numeric(false, function() {
		alert("Integers only");
		this.value = "";
		this.focus();
	});
	$("#cc_cvc").numeric(false, function() {
		alert("Integers only");
		this.value = "";
		this.focus();
	});
    /*http://jquerycreditcardvalidator.com/#supported_card_types*/
	$("#cc_card_number").validateCreditCard(function(e) {
			if (e.card_type == null) {
				$("ul.cards li").removeClass("off");
				$("#cc_card_number").removeClass("valid_cc");
				$(".vertical.maestro").slideUp({
					duration: 200
				}).animate({
					opacity: 0
				}, {
					queue: !1,
					duration: 200
				});
				return
			}
			$("ul.cards li").addClass("off");
			$("ul.cards ." + e.card_type.name).removeClass("off");
			e.card_type.name === "maestro" ? $(".vertical.maestro").slideDown({
				duration: 200
			}).animate({
				opacity: 1
			}, {
				queue: !1
			}) : $(".vertical.maestro").slideUp({
				duration: 200
			}).animate({
				opacity: 0
			}, {
				queue: !1,
				duration: 200
			});
			return e.length_valid && e.luhn_valid ? $("#cc_card_number").addClass("valid_cc") : $("#cc_card_number").removeClass("valid_cc")
		}, {
			//accept: ["visa", "visa_electron", "mastercard", "maestro", "discover"]
            accept: ["visa", "mastercard"]
	});
});
function showpaymentinfo(obj) {
$('#divPayment').find('div.sub-payment-info').hide();
$('#divPayment').find('div[name=' + obj.value + ']').show();
//var divContent = $('#h-shopping-cart').find('div.sub-payment-info');
// if (divContent)
// {
// $('#h-shopping-cart').find('div.content').hide();
// divContent.show();
// }
}